/**
 * Copyright (C) 2011 Audit Logger Project. All rights reserved.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.auditlogger.security;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.auditlogger.config.ConfigurationServices;

import sun.misc.BASE64Encoder;

class Digester {
	
	static String digest(String message) throws InvalidAlgorithmException {
		String result = message;
		try {
			MessageDigest messageDigest = MessageDigest.getInstance(
					ConfigurationServices.getPropertiesConfigurator().getMessageDigestAlgorithm());
			messageDigest.update(message.getBytes());
			byte[] digest = messageDigest.digest();
			
			// Encode digest using Base64 
			result = new BASE64Encoder().encode(digest);
		} catch (NoSuchAlgorithmException e) {
			throw new InvalidAlgorithmException (e.getMessage(), e);
		}
		return result;
	}
}
